package com.study.sbsummary.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description:
 * @Author: hushichao
 * @Date: 2022-02-21
 */
public class DocImageUrlsUtil {

    /**
     * 1、{"http:\/\/i1.go2yd.com\/image\/ZL_cnt_1_018V30iWfBHO":{"w":2160,"h":2880,"format":"JPEG"}}
     *   或者 {"https:\/\/si1.go2yd.com\/get-image\/0vpnoqMqhvn1":{"w":1080,"h":1440,"format":"JPEG"},"https:\/\/si1.go2yd.com\/get-image\/0vpnoqMqhvn2":{"w":1080,"h":1440,"format":"JPEG"},"https:\/\/si1.go2yd.com\/get-image\/0vpnoqMqhvn3":{"w":1080,"h":1440,"format":"JPEG"},"https:\/\/si1.go2yd.com\/get-image\/0vpnoqMqhvn4":{"w":1080,"h":1440,"format":"JPEG"},"https:\/\/si1.go2yd.com\/get-image\/0vpnoqMqhvn5":{"w":1080,"h":1440,"format":"JPEG"},"https:\/\/si1.go2yd.com\/get-image\/0vpnoqMqhvn6":{"w":1080,"h":1440,"format":"JPEG"},"https:\/\/si1.go2yd.com\/get-image\/0vpnoqMqhvn7":{"w":1080,"h":1440,"format":"JPEG"},"https:\/\/si1.go2yd.com\/get-image\/0vpnoqMqhvn8":{"w":1080,"h":1440,"format":"JPEG"}}
     *
     * 2、{"format":"JPEG","h":1920,"url":"https://si1.go2yd.com/get-image/0w8IJOdlK2T","w":1440}
     *
     * 3、["https://si1.go2yd.com/get-image/0tkhbA0Al6G"]
     *
     * 4、{"ZL_cnt_0_016TmlceoVpN":{"w":2160,"h":2880,"format":"JPEG"}}
     *
     */

    public static ImageUrls getImageUrls(String imageUrlsStr) {
        ImageUrls imageUrls = new ImageUrls();
        if (StringUtils.isBlank(imageUrlsStr)) {
            return imageUrls;
        }
        try {
            //1或者4
            Map map = (Map) JSONObject.parse(imageUrlsStr);
            for (Object entry : map.entrySet()) {
                String urlStr = ((Map.Entry) entry).getKey().toString();
                imageUrls.setUrl(urlStr);
                if (!urlStr.contains("http")) {
                    imageUrls.setUrl("http://i1.go2yd.com/image/" + urlStr);
                }
                Object value = ((Map.Entry) entry).getValue();
                Map mapStr = (Map) JSONObject.parse(value.toString());
                setImageUrlsByMap(imageUrls, mapStr);
                return imageUrls;
            }
        } catch (Exception e) {
            try {
                //2
                Map map = (Map) JSONObject.parse(imageUrlsStr);
                imageUrls.setUrl(map.get("url").toString());
                setImageUrlsByMap(imageUrls, map);
                return imageUrls;
            } catch (Exception e1) {
                try {
                    //3、["https://si1.go2yd.com/get-image/0tkhbA0Al6G"]
                    List<String> list = JSONObject.parseArray(imageUrlsStr, String.class);
                    imageUrls.setUrl(list.get(0));
                } catch (Exception e2) {
                    return imageUrls;
                }
            }
        }
        return imageUrls;
    }

    private static void setImageUrlsByMap(ImageUrls imageUrls, Map map) {
        Object wObj = map.get("w");
        Object hObj = map.get("h");
        Object formatObj = map.get("format");
        if (Objects.isNull(wObj)) {
            wObj = "0";
        }
        if (Objects.isNull(hObj)) {
            hObj = "0";
        }
        if (Objects.isNull(formatObj)) {
            formatObj = "";
        }
        imageUrls.setW(Integer.valueOf(wObj.toString()));
        imageUrls.setH(Integer.valueOf(hObj.toString()));
        imageUrls.setFormat(formatObj.toString());
    }

    @Data
    public static class ImageUrls {
        String url = "";
        Integer w = 0;
        Integer h = 0;
        String format = "";
    }

    private static void parse() {

    }

    public static List<ImageUrls> getImageUrlsList(String imageUrlsStr) {
        List<ImageUrls> imageUrlsList = Lists.newArrayList();
        if (StringUtils.isBlank(imageUrlsStr)) {
            return imageUrlsList;
        }
        List<Map> list = (List<Map>) JSONObject.parseArray(imageUrlsStr, Map.class);
        for (Map map: list) {
            ImageUrls imageUrls = new ImageUrls();
            for (Object entry : map.entrySet()) {
                imageUrls.setUrl(((Map.Entry) entry).getKey().toString());
                Object value = ((Map.Entry) entry).getValue();
                Map mapStr = (Map) JSONObject.parse(value.toString());
                Object wObj = mapStr.get("w");
                Object hObj = mapStr.get("h");
                Object formatObj = mapStr.get("format");
                if (Objects.isNull(wObj)) {
                    wObj = "0";
                }
                if (Objects.isNull(hObj)) {
                    hObj = "0";
                }
                if (Objects.isNull(formatObj)) {
                    formatObj = "";
                }
                imageUrls.setW(Integer.valueOf(wObj.toString()));
                imageUrls.setH(Integer.valueOf(hObj.toString()));
                imageUrls.setFormat(formatObj.toString());
            }
            imageUrlsList.add(imageUrls);
        }

        return imageUrlsList;
    }



}
